package com.ruicar.afs.cloud.manage.exemptconfig.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.core.log.annotation.SysLog;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.CostExemptTypeEnum;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import com.ruicar.afs.cloud.manage.common.entity.ManageCostExemptConfig;
import com.ruicar.afs.cloud.manage.common.service.ManageCostExemptConfigService;
import com.ruicar.afs.cloud.manage.exemptconfig.vo.ManageCostExemptConfigVo;
import com.ruicar.afs.cloud.manage.workflow.processor.ApproveTaskProcessor;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import sun.text.resources.FormatData;

import java.math.BigDecimal;
import java.sql.Wrapper;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author huangtao
 * @create 2020/9/23
 * 客户信息变更模块
 */
@Slf4j
@RestController
@RequestMapping("/exemptConfig")
@AllArgsConstructor
public class ExemptConfigController {
    private final ManageCostExemptConfigService manageCostExemptConfigService;
    private final ApproveTaskProcessor approveTaskProcessor;

    @ApiOperation(value = "获取用户豁免配置")
    @PostMapping("/getConfigListData")
    public IResponse<IPage<ManageCostExemptConfig>> getConfigListData(@RequestBody ManageCostExemptConfigVo vo) {
        Page<ManageCostExemptConfig> list = manageCostExemptConfigService.page(new Page(vo.getPageNumber(), vo.getPageSize()), Wrappers.<ManageCostExemptConfig>query().lambda()

                .eq(ManageCostExemptConfig::getType, CostExemptTypeEnum.single)
                .like(EmptyUtils.isNotEmpty(vo.getUser()), ManageCostExemptConfig::getUser, vo.getUser())
        );
        return new IResponse().setData(list);
    }

    @Transactional
    @SysLog("根据ID删除费用用户配置")
    @ApiOperation(value = "通过id删除")
    @RequestMapping(value = "/deleteExemptConfig/{id}", method = RequestMethod.DELETE)
    public IResponse<Boolean> delById(@PathVariable String id) {
        manageCostExemptConfigService.removeById(id);
        return new IResponse<Boolean>().setMsg("删除成功");
    }

    @ApiOperation(value = "豁免用户配置保存")
    @PostMapping("/save")
    public IResponse userSave(@RequestBody ManageCostExemptConfigVo vo) {

        if (EmptyUtils.isNotEmpty(vo.getId())) {
            vo.setType(CostExemptTypeEnum.single);
            vo.setUser(vo.getUsername());
            ManageCostExemptConfig one = manageCostExemptConfigService.getOne(Wrappers.<ManageCostExemptConfig>query().lambda()
                    .eq(ManageCostExemptConfig::getId, vo.getId())
            );
//            if (one.getLimitAmt().compareTo(vo.getLimitAmt())==1){
//                throw new AfsBaseException("用户的单体限额只允许增加");
//            }

            BigDecimal LimitAmtResidue  =BigDecimal.ZERO;
            LimitAmtResidue  = vo.getLimitAmt().subtract(one.getLimitAmt());
            vo.setSurplusLimitAmt(vo.getSurplusLimitAmt().add(LimitAmtResidue));
            manageCostExemptConfigService.updateById(vo);
            return new IResponse().setMsg("修改成功");
        } else {
            vo.setType(CostExemptTypeEnum.single);
            vo.setUser(vo.getUsername());
            vo.setSurplusLimitAmt(vo.getLimitAmt());
            List<ManageCostExemptConfig> list = manageCostExemptConfigService.list(Wrappers.<ManageCostExemptConfig>query().lambda()
                    .eq(ManageCostExemptConfig::getUser, vo.getUser())
            );
            if (list.size() > 0) {
                for (ManageCostExemptConfig manageCostExemptConfig : list) {
                    if (manageCostExemptConfig.getCostType().equals(vo.getCostType())) {
                        throw new AfsBaseException("同一个用户不能再次新增同一费用类型单笔合同最高限额");
                    }
                }
            }
            manageCostExemptConfigService.save(vo);
            return new IResponse().setMsg("保存成功");
        }
    }

    @ApiOperation(value = "获取部门豁免配置")
    @PostMapping("/getDepConfigListData")
    public IResponse<IPage<ManageCostExemptConfig>> getDepConfigListData(@RequestBody ManageCostExemptConfigVo vo) {
        Page<ManageCostExemptConfig> list = manageCostExemptConfigService.page(new Page(vo.getPageNumber(), vo.getPageSize()), Wrappers.<ManageCostExemptConfig>query().lambda()
                .eq(ManageCostExemptConfig::getType, CostExemptTypeEnum.total)
                .like(EmptyUtils.isNotEmpty(vo.getUser()), ManageCostExemptConfig::getUser, vo.getUser())
        );
        return new IResponse().setData(list);
    }


    @ApiOperation(value = "豁免部门配置保存")
    @PostMapping("/saveDep")
    public IResponse saveDep(@RequestBody ManageCostExemptConfigVo vo) {

        if (EmptyUtils.isNotEmpty(vo.getId())) {
            vo.setUser(vo.getDeptId().toString());
            ManageCostExemptConfig one = manageCostExemptConfigService.getOne(Wrappers.<ManageCostExemptConfig>query().lambda()
                    .eq(ManageCostExemptConfig::getId, vo.getId())
            );
            if (one.getLimitAmt().compareTo(vo.getLimitAmt())==1){
                throw new AfsBaseException("部门豁免的整体限额只允许增加");
            }

            BigDecimal LimitAmtResidue  =BigDecimal.ZERO;
           LimitAmtResidue  = vo.getLimitAmt().subtract(one.getLimitAmt());
            vo.setSurplusLimitAmt(vo.getSurplusLimitAmt().add(LimitAmtResidue));
            manageCostExemptConfigService.updateById(vo);
            return new IResponse().setMsg("修改成功");
        } else {
            vo.setType(CostExemptTypeEnum.total);
            vo.setUser(vo.getDeptId().toString());
//            Date yearDate = vo.getYearDate();
            SimpleDateFormat yyyy = new SimpleDateFormat("yyyy");
//            String format = yyyy.format(yearDate);
//            vo.setYear(Integer.parseInt(format));
            vo.setYear(vo.getYearDate());
            List<ManageCostExemptConfig> list = manageCostExemptConfigService.list(Wrappers.<ManageCostExemptConfig>query().lambda()
                    .eq(ManageCostExemptConfig::getUser, vo.getUser())
                    .eq(ManageCostExemptConfig::getYear, vo.getYear())
            );
            if (list.size() > 0) {
                for (ManageCostExemptConfig manageCostExemptConfig : list) {
                    if (manageCostExemptConfig.getCostType().equals(vo.getCostType())) {
                        throw new AfsBaseException("同一个部门不能在同一年再次新增相同费用类型的整体限额");
                    }
                }
            }
            vo.setSurplusLimitAmt(vo.getLimitAmt());
            manageCostExemptConfigService.save(vo);
            return new IResponse().setMsg("保存成功");
        }
    }
}
